


from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU


class OrderSimpleModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time'
        ]


class SKUSimpleModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image',
        ]

# 订单商品表
class OrderGoodsModelSerializer(serializers.ModelSerializer):

    # 1、SKU主表对象；2、单个
    # 3、自定义
    sku = SKUSimpleModelSerializer()

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku'
        ]

# 额外在去专门定义一个序列化器
# 序列化订单详细数据
# 针对同一个模型类，可以定义多个序列化器完成不同的序列化操作
class OrderDetailModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()
    # 1、OrderGoods模型类对象；2、多个对象
    # 3、自定义序列化(结果是自定义)
    skus = OrderGoodsModelSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = '__all__'

















